/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

package org.vineatoum.view;

import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JSeparator;
import java.awt.Color;

import org.vineatoum.business.Cuve;
import org.vineatoum.business.Exploitation;
import org.vineatoum.business.Parcelle;
import org.vineatoum.business.Plantation;
import org.vineatoum.controller.ExploitationController;
import org.vineatoum.controller.IExploitationController;
import org.vineatoum.model.abstraction.IVineaToumModel;
import org.vineatoum.observer.IObserverExploitation;
import org.vineatoum.utils.Address;
import org.vineatoum.view.data.CuveDataPanel;
import org.vineatoum.view.data.ParcelleDataPanel;
import org.vineatoum.view.data.PlantationDataPanel;
import org.vineatoum.view.utils.JPanelWrapped;
import org.vineatoum.view.utils.jtablecustom.JTableCustom;
import org.vineatoum.view.utils.jtablecustom.OwnerTable;

public class ExploitationPanel extends JPanelWrapped implements IObserverExploitation {
	private static final long serialVersionUID = 59119799447459147L;
	
	private JTextField textFieldName;
	private JTextField textFieldLegalForm;
	private JTextField textFieldRegistrationNumber;
	private JSeparator separator;
	private JTextField textFieldStreetNumber;
	private JTextField textFieldStreetName;
	private JTextField textFieldZipCode;
	private JTextField textFieldCity;
	private JTableCustom<OwnerTable> tableOwners;
	private JTableCustom<Parcelle> tableParcelles;
	private JTableCustom<Plantation> tablePlantations;
	private JTableCustom<Cuve> tableCuves;
	
	private IExploitationController controller;
	private IVineaToumModel model;

	/**
	 * Create the dialog.
	 * @param model 
	 */
	public ExploitationPanel(IVineaToumModel _model, Exploitation exploitation) {
		this.model = _model;
		controller = new ExploitationController(model);
		
		model.addObserver(this);
				
		setBounds(100, 100, 582, 279);
		this.setBorder(new EmptyBorder(5, 5, 5, 5));
		{
			GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
			gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel.gridx = 0;
			gbc_lblNewLabel.gridy = 1;
		}
		setLayout(new BorderLayout(0, 0));
		{
			GridBagLayout gbl_panel_2 = new GridBagLayout();
			gbl_panel_2.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0 };
			gbl_panel_2.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
					0, 0, 0, 0, 0, 0, 0, 0, 0 };
			gbl_panel_2.columnWeights = new double[] { 0.0, 1.0, 1.0, 0.0, 1.0,
					1.0, Double.MIN_VALUE };
			gbl_panel_2.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
					0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
					1.0, Double.MIN_VALUE };
			setLayout(gbl_panel_2);
			{
				JLabel lblName = new JLabel("Nom");
				GridBagConstraints gbc_label = new GridBagConstraints();
				gbc_label.anchor = GridBagConstraints.EAST;
				gbc_label.insets = new Insets(0, 0, 5, 5);
				gbc_label.gridx = 0;
				gbc_label.gridy = 0;
				add(lblName, gbc_label);
			}
			{
				textFieldName = new JTextField();
				textFieldName.setColumns(10);
				GridBagConstraints gbc_textField = new GridBagConstraints();
				gbc_textField.gridwidth = 4;
				gbc_textField.insets = new Insets(0, 0, 5, 5);
				gbc_textField.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField.gridx = 1;
				gbc_textField.gridy = 0;
				add(textFieldName, gbc_textField);
			}
			{
				JLabel lblRegistrationNumber = new JLabel(
						"Num\u00E9ro d'identification");
				GridBagConstraints gbc_label_2 = new GridBagConstraints();
				gbc_label_2.anchor = GridBagConstraints.EAST;
				gbc_label_2.insets = new Insets(0, 0, 5, 5);
				gbc_label_2.gridx = 0;
				gbc_label_2.gridy = 1;
				add(lblRegistrationNumber, gbc_label_2);
			}
			{
				textFieldRegistrationNumber = new JTextField();
				textFieldRegistrationNumber.setColumns(10);
				GridBagConstraints gbc_textField_2 = new GridBagConstraints();
				gbc_textField_2.insets = new Insets(0, 0, 5, 5);
				gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_2.gridx = 1;
				gbc_textField_2.gridy = 1;
				add(textFieldRegistrationNumber, gbc_textField_2);
			}
			{
				JLabel lblLegalForm = new JLabel("Forme Juridique");
				GridBagConstraints gbc_label_1 = new GridBagConstraints();
				gbc_label_1.anchor = GridBagConstraints.EAST;
				gbc_label_1.insets = new Insets(0, 0, 5, 5);
				gbc_label_1.gridx = 3;
				gbc_label_1.gridy = 1;
				add(lblLegalForm, gbc_label_1);
			}
			{
				textFieldLegalForm = new JTextField();
				textFieldLegalForm.setColumns(10);
				GridBagConstraints gbc_textField_1 = new GridBagConstraints();
				gbc_textField_1.insets = new Insets(0, 0, 5, 5);
				gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_1.gridx = 4;
				gbc_textField_1.gridy = 1;
				add(textFieldLegalForm, gbc_textField_1);
			}
			{
				separator = new JSeparator();
				separator.setForeground(Color.GRAY);
				separator.setBackground(Color.GRAY);
				GridBagConstraints gbc_separator = new GridBagConstraints();
				gbc_separator.fill = GridBagConstraints.HORIZONTAL;
				gbc_separator.gridwidth = 3;
				gbc_separator.insets = new Insets(0, 0, 5, 5);
				gbc_separator.gridx = 1;
				gbc_separator.gridy = 2;
				add(separator, gbc_separator);
			}
			{
				JLabel lblAddress = new JLabel("Adresse");
				GridBagConstraints gbc_label_3 = new GridBagConstraints();
				gbc_label_3.anchor = GridBagConstraints.EAST;
				gbc_label_3.insets = new Insets(0, 0, 5, 5);
				gbc_label_3.gridx = 0;
				gbc_label_3.gridy = 3;
				add(lblAddress, gbc_label_3);
			}
			{
				JLabel lblStreetNumber = new JLabel("N\u00B0 Rue");
				GridBagConstraints gbc_label_4 = new GridBagConstraints();
				gbc_label_4.anchor = GridBagConstraints.EAST;
				gbc_label_4.insets = new Insets(0, 0, 5, 5);
				gbc_label_4.gridx = 0;
				gbc_label_4.gridy = 4;
				add(lblStreetNumber, gbc_label_4);
			}
			{
				textFieldStreetNumber = new JTextField();
				textFieldStreetNumber.setColumns(10);
				GridBagConstraints gbc_textField_3 = new GridBagConstraints();
				gbc_textField_3.insets = new Insets(0, 0, 5, 5);
				gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_3.gridx = 1;
				gbc_textField_3.gridy = 4;
				add(textFieldStreetNumber, gbc_textField_3);
			}
			{
				JLabel lblStreetName = new JLabel("Rue");
				GridBagConstraints gbc_label_5 = new GridBagConstraints();
				gbc_label_5.anchor = GridBagConstraints.EAST;
				gbc_label_5.insets = new Insets(0, 0, 5, 5);
				gbc_label_5.gridx = 3;
				gbc_label_5.gridy = 4;
				add(lblStreetName, gbc_label_5);
			}
			{
				textFieldStreetName = new JTextField();
				textFieldStreetName.setColumns(10);
				GridBagConstraints gbc_textField_4 = new GridBagConstraints();
				gbc_textField_4.insets = new Insets(0, 0, 5, 5);
				gbc_textField_4.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_4.gridx = 4;
				gbc_textField_4.gridy = 4;
				add(textFieldStreetName, gbc_textField_4);
			}
			{
				JLabel lblZipCode = new JLabel("Code Postal");
				GridBagConstraints gbc_label_6 = new GridBagConstraints();
				gbc_label_6.anchor = GridBagConstraints.EAST;
				gbc_label_6.insets = new Insets(0, 0, 5, 5);
				gbc_label_6.gridx = 0;
				gbc_label_6.gridy = 5;
				add(lblZipCode, gbc_label_6);
			}
			{
				textFieldZipCode = new JTextField();
				textFieldZipCode.setColumns(10);
				GridBagConstraints gbc_textField_5 = new GridBagConstraints();
				gbc_textField_5.insets = new Insets(0, 0, 5, 5);
				gbc_textField_5.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_5.gridx = 1;
				gbc_textField_5.gridy = 5;
				add(textFieldZipCode, gbc_textField_5);
			}
			{
				JLabel lblCity = new JLabel("Ville");
				GridBagConstraints gbc_label_7 = new GridBagConstraints();
				gbc_label_7.anchor = GridBagConstraints.EAST;
				gbc_label_7.insets = new Insets(0, 0, 5, 5);
				gbc_label_7.gridx = 3;
				gbc_label_7.gridy = 5;
				add(lblCity, gbc_label_7);
			}
			{
				textFieldCity = new JTextField();
				textFieldCity.setColumns(10);
				GridBagConstraints gbc_textField_6 = new GridBagConstraints();
				gbc_textField_6.insets = new Insets(0, 0, 5, 5);
				gbc_textField_6.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField_6.gridx = 4;
				gbc_textField_6.gridy = 5;
				add(textFieldCity, gbc_textField_6);
			}
			{
				JLabel lblOwners = new JLabel("Propri\u00E9taires");
				GridBagConstraints gbc_label_8 = new GridBagConstraints();
				gbc_label_8.anchor = GridBagConstraints.EAST;
				gbc_label_8.insets = new Insets(0, 0, 5, 5);
				gbc_label_8.gridx = 0;
				gbc_label_8.gridy = 6;
				add(lblOwners, gbc_label_8);
			}
			{
				tableOwners = new JTableCustom<OwnerTable>(new OwnerTable());
				tableOwners.setVisible(true);
				GridBagConstraints gbc_table = new GridBagConstraints();
				gbc_table.insets = new Insets(0, 0, 5, 5);
				gbc_table.fill = GridBagConstraints.BOTH;
				gbc_table.gridwidth = 4;
				gbc_table.gridx = 1;
				gbc_table.gridy = 6;
				JPanel panel = new JPanel(new BorderLayout());
				panel.add(tableOwners, BorderLayout.CENTER);
				panel.add(tableOwners.getTableHeader(), BorderLayout.NORTH);
				add(panel, gbc_table);
			}
			{
				JLabel lblParcelles = new JLabel("Parcelles");
				GridBagConstraints gbc_label = new GridBagConstraints();
				gbc_label.anchor = GridBagConstraints.EAST;
				gbc_label.insets = new Insets(0, 0, 5, 5);
				gbc_label.gridx = 0;
				gbc_label.gridy = 7;
				add(lblParcelles, gbc_label);
			}
			{				
				tableParcelles = new JTableCustom<Parcelle>(new Parcelle()) {
					private static final long serialVersionUID = 1L;

						protected boolean fireEditingStarted(final JTableCustom<Parcelle> table, final int row, int column) {
							table.getCellEditor().stopCellEditing();
							final ParcelleDataPanel parcellePanel = new ParcelleDataPanel(model, (Parcelle) table.getValueAt(row));
							Object[] options = {"OK", "Annuler"};
							int n = JOptionPane.showOptionDialog(getRootPane(), parcellePanel, "Ajouter", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
							if(n == JOptionPane.OK_OPTION) {
								table.setValueAt(parcellePanel.getTextFieldName().getText(), row, 0);
								table.setValueAt(parcellePanel.getTextFieldCadastraNumber().getText(), row, 1);
								table.setValueAt(parcellePanel.getComboBoxLieuDit().getSelectedItem(), row, 2);
//								table.setValueAt(parcellePanel.comboBoxCommune.getSelectedItem(), row, 3);
								//table.setValueAt(parcellePanel.textFieldGPS.getSelectedItem(), row, 1);
								table.setValueAt(parcellePanel.getSpinnerSurface().getValue(), row, 3);
								table.setValueAt(parcellePanel.getComboBoxSoilType().getSelectedItem(), row, 4);
							}
							
							return false;
						};
				};
				tableParcelles.setVisible(true);
				GridBagConstraints gbc_table = new GridBagConstraints();
				gbc_table.insets = new Insets(0, 0, 5, 5);
				gbc_table.fill = GridBagConstraints.BOTH;
				gbc_table.gridwidth = 4;
				gbc_table.gridx = 1;
				gbc_table.gridy = 7;
				JPanel panel = new JPanel(new BorderLayout());
				panel.add(tableParcelles, BorderLayout.CENTER);
				panel.add(tableParcelles.getTableHeader(), BorderLayout.NORTH);
				add(panel, gbc_table);
			}
			{
				JLabel lblPlantations = new JLabel("Plantations");
				GridBagConstraints gbc_label = new GridBagConstraints();
				gbc_label.anchor = GridBagConstraints.EAST;
				gbc_label.insets = new Insets(0, 0, 5, 5);
				gbc_label.gridx = 0;
				gbc_label.gridy = 8;
				add(lblPlantations, gbc_label);
			}
			{				
				tablePlantations = new JTableCustom<Plantation>(new Plantation()) {
					private static final long serialVersionUID = 1L;

						protected boolean fireEditingStarted(final JTableCustom<Plantation> table, final int row, int column) {
							table.getCellEditor().stopCellEditing();
							final PlantationDataPanel parcellePanel = new PlantationDataPanel(model, (Plantation) table.getValueAt(row));
							Object[] options = {"OK", "Annuler"};
							int n = JOptionPane.showOptionDialog(getRootPane(), parcellePanel, "Ajouter", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
							if(n == JOptionPane.OK_OPTION) {
								table.setValueAt(parcellePanel.getTextFieldName().getText(), row, 0);
								table.setValueAt(parcellePanel.getComboBoxCepage().getSelectedItem(), row, 1);
								table.setValueAt(parcellePanel.getComboBoxAppelation().getSelectedItem(), row, 2);
								table.setValueAt(parcellePanel.getSpinnerSurface().getValue(), row, 3);
								table.setValueAt(parcellePanel.getYearChooserPlantationYear().getYear(), row, 4);
								table.setValueAt(parcellePanel.getSpinnerCepGap().getValue(), row, 5);
								table.setValueAt(parcellePanel.getSpinnerRowGap().getValue(), row, 6);
								table.setValueAt(parcellePanel.getSpinnerCepDensity().getValue(), row, 7);
								table.setValueAt(parcellePanel.getListBoxParcelles().getSelectedValues(), row, 8);
							}
							
							return false;
						};
				};
				tablePlantations.setVisible(true);
				GridBagConstraints gbc_table = new GridBagConstraints();
				gbc_table.insets = new Insets(0, 0, 5, 5);
				gbc_table.fill = GridBagConstraints.BOTH;
				gbc_table.gridwidth = 4;
				gbc_table.gridx = 1;
				gbc_table.gridy = 8;
				JPanel panel = new JPanel(new BorderLayout());
				panel.add(tablePlantations, BorderLayout.CENTER);
				panel.add(tablePlantations.getTableHeader(), BorderLayout.NORTH);
				add(panel, gbc_table);
			}	
			{
				JLabel lblCuves = new JLabel("Cuves");
				GridBagConstraints gbc_label = new GridBagConstraints();
				gbc_label.anchor = GridBagConstraints.EAST;
				gbc_label.insets = new Insets(0, 0, 5, 5);
				gbc_label.gridx = 0;
				gbc_label.gridy = 9;
				add(lblCuves, gbc_label);
			}
			{				
				tableCuves = new JTableCustom<Cuve>(new Cuve()) {
					private static final long serialVersionUID = 1L;

						protected boolean fireEditingStarted(final JTableCustom<Cuve> table, final int row, int column) {
							table.getCellEditor().stopCellEditing();
							final CuveDataPanel cuvePanel = new CuveDataPanel(model, (Cuve) table.getValueAt(row));
							Object[] options = {"OK", "Annuler"};
							int n = JOptionPane.showOptionDialog(getRootPane(), cuvePanel, "Ajouter", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
							if(n == JOptionPane.OK_OPTION) {
								table.setValueAt(cuvePanel.getTextFieldName().getText(), row, 0);
								table.setValueAt(cuvePanel.getSpinnerSize().getValue(), row, 1);
								table.setValueAt(cuvePanel.getComboBoxKind().getSelectedItem(), row, 2);
								table.setValueAt(cuvePanel.getCheckBoxChapeauFlottant().isSelected(), row, 3);
							}
							
							return false;
						};
				};
				tableCuves.setVisible(true);
				GridBagConstraints gbc_table = new GridBagConstraints();
				gbc_table.insets = new Insets(0, 0, 5, 5);
				gbc_table.fill = GridBagConstraints.BOTH;
				gbc_table.gridwidth = 4;
				gbc_table.gridx = 1;
				gbc_table.gridy = 9;
				JPanel panel = new JPanel(new BorderLayout());
				panel.add(tableCuves, BorderLayout.CENTER);
				panel.add(tableCuves.getTableHeader(), BorderLayout.NORTH);
				add(panel, gbc_table);
			}	
			
			updateFields(exploitation);
		}
	}
	
	public void updateFields(Exploitation exploitation) {		
		textFieldName.setText(exploitation.getName());
		
		Address address = exploitation.getAddress();
		textFieldStreetNumber.setText(address.getStreetNumber());
		textFieldStreetName.setText(address.getStreet());
		textFieldZipCode.setText(address.getZipCode());
		textFieldCity.setText(address.getCity());
		
		textFieldLegalForm.setText(exploitation.getLegalForm());
		textFieldRegistrationNumber.setText(exploitation.getRegistrationNumber());
		textFieldStreetNumber.setText(exploitation.getAddress().getStreetNumber());
		textFieldStreetName.setText(exploitation.getAddress().getStreet());
		textFieldZipCode.setText(exploitation.getAddress().getZipCode());
		textFieldCity.setText(exploitation.getAddress().getCity());
		tableOwners.setData(exploitation.getOwnersData());
		tableParcelles.setData(exploitation.getParcelles());
		tablePlantations.setData(exploitation.getPlantations());
		tableCuves.setData(exploitation.getCuves());
	}

	@Override
	public void doWhenWrapperOkClick() {
		System.out.println("Exploitation Recieve Ok Click From Wrapper");
		Exploitation exploitation = new Exploitation();
		
		exploitation.setName(textFieldName.getText());
		
		Address address = new Address();
		address.setStreetNumber(textFieldStreetNumber.getText());
		address.setStreet(textFieldStreetName.getText());
		address.setZipCode(textFieldZipCode.getText());
		address.setCity(textFieldCity.getText());
		exploitation.setAddress(address);
		
		exploitation.setLegalForm(textFieldLegalForm.getText());
		exploitation.setRegistrationNumber(textFieldRegistrationNumber.getText());
//		exploitation.setNumberOfEmployees(); TODO
//		exploitation.setLogo(logo) TODO
		exploitation.setOwnersData(tableOwners.getData());
		exploitation.setPlantations(tablePlantations.getData());
		exploitation.setParcelles(tableParcelles.getData());
		exploitation.setCuves(tableCuves.getData());
		
		controller.updateExploitation(exploitation);
		
	}

	@Override
	public void doWhenWrapperCancelClick() {
		// TODO Auto-generated method stub
		System.out.println("Exploitation Recieve  Click From Wrapper");
		getParent().remove(this);
	}
}
